<?php
namespace Admin\Model;
use Think\Model;
class WxauthoriseModel extends Model {

   public function _initialize()  
    {  
		header("Content-Type: text/html; charset=utf-8");

    	if(!session('user')){
    		$message=D('User')->loginout(2);
    		$this->success($message,U('index/index'));
    	}
    } 

	/**
	 * [add_wechat description]
	 *
	 * 说明：
	 * 不能指定位置或者没有对应表模型
	 *webuser ->manage
	 *
	 * 
	 */
	
	public function manage_diqu_chanpin(){
		$bm_goods_gx = M('goods_gx'); 		
		$bm_goods_foundation = M('goods_foundation');		
		$Gx = $bm_goods_gx->field('id')->where('shifou = 2')->select();
		/*****************联表查询**********************************/
		if($_GET['search']){
			$search=$_GET['search'];
			$where['name']=array('LIKE',"%".$search."%");
			if(!$_GET['p'])$_get['p']=1;
			$data = M('user');
			$count = $bm_goods_gx
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.id=b.gx_id','LEFT')
				 ->field('b.name,b.dtypename,b.qudaoname,a.id')
				 ->group('a.id')
				 ->where("a.shifou = 2")
				 ->where($where)
				 ->count();
			$Page       = new \Think\Page($count,10);
			$show       = $Page->show();
			$list=$bm_goods_gx
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.id=b.gx_id','LEFT')
				 ->field('b.name,b.dtypename,b.qudaoname,a.id')
				 ->where("a.shifou = 2")
				 ->where($where)
				 ->group('a.id')
				 ->limit($Page->firstRow.','.$Page->listRows)
				 ->select();
		}
		else{
			if(!$_GET['p'])$_get['p']=1;
			$count = $bm_goods_gx
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.id=b.gx_id','LEFT')
				 ->field('b.name,b.dtypename,b.qudaoname,a.id')
				 ->group('a.id')
				 ->where("a.shifou = 2")
				 ->count();
			$Page       = new \Think\Page($count,10);
			$show       = $Page->show();
			$list=$bm_goods_gx
				 ->alias('a')
				 ->join('bm_goods_foundation b ON a.id=b.gx_id','LEFT')
				 ->field('b.name,b.dtypename,b.qudaoname,a.id')
				 ->where("a.shifou = 2")
				 ->group('a.id')
				 ->limit($Page->firstRow.','.$Page->listRows)
				 ->select();
		}
		return array("count"=>$count,"list"=>$list,"show"=>$show);

	}
	/**
	 * 公众号注册绑定数据关联
	 * @DateTime:    [2018-05-21 10:52:20]
	 */
	public function add_wechat(){

			$bm_wechat=M('wechat');
			$user=M('user');
			$data1['appid']=$_POST['appid'];
			$data1['appsecret']=$_POST['appsecret'];
			$data1['token']=$_POST['token'];
			$data1['EncodingAESKey']=$_POST['EncodingAESKey'];
			$data1['original_id']=$_POST['original_id'];
			$data1['nick_name']=$_POST['nick_name'];
			$data1['u_pro_id'] = $_SESSION['user']['u_pro_id'];
			$data1['u_city_id'] = $_SESSION['user']['u_city_id'];
			$data1['date']=date("Y-m-d H:i:s");
			$result=$bm_wechat->add($data1);
			if($result){
				$data2['wechat_id']=$result;
				$data2['u_name']=$_POST['u_name'];
				$data2['u_phone']=$_POST['u_phone'];
				$data2['u_email']=$_POST['u_email'];
				$data2['u_pass']=$_POST['u_pass'];
				$data2['code']=rand(10000,99999);
				$data2['u_time'] = date("Y-m-d H:i:s");

				/***************获取当前session数据获取绑定**********/
				$data2['u_type'] = $_SESSION['user']['u_type']+1;
				$data2['p_id'] = $_SESSION['user']['u_id'];
				$data2['u_pro_id'] = $_SESSION['user']['u_pro_id'];
				$data2['u_city_id'] = $_SESSION['user']['u_city_id'];
				$data2['u_area_id'] = $_SESSION['user']['u_area_id'];
				/***************获取当前session数据获取绑定**********/
				$res=$user->add($data2);
				if($res){
						$my_user= $user->where("u_id=$res")->find();
						//$user_update['u_pass']=md5(md5($_POST['u_pass']).$my_user['code']);//MD5加密
						$user_update['u_pass']=$_POST['u_pass'];//$my_user['code'];
						$user->where("u_id=$res")->save($user_update);
						$wechatlink['link']='index.php/Admin/Wchat/index?wechat_id='.$result;
						$bm_wechat->where("wechat_id=$result")->save($wechatlink);
						$msg['code']=1;
						$msg['message']='添加成功';
						$msg['link']=$wechatlink['link'];
						$message="恭喜".$_POST['u_name']."成为评估令用户";
						send_email($_POST['u_email'],$message);
						
				}
				else{
					$bm_wechat->delete($result);
					$msg['code']=2;
					$msg['message']='用户插入失败';
				}
			}
			else{
				$msg['code']=3;
				$msg['message']='公众号插入失败';
			}
			return $msg;

	}
/*************以下方法隶属Manageme同名操作****************************/

	public function manage_me(){
		$data = M('web_user');
		if($_GET['search']){
			$search=$_GET['search'];
			$oa['web_phone']=array('like','%'.$search.'%');
			$oa['web_name']=array('like','%'.$search.'%');
			$oa['_logic'] = 'or';
			$where['_complex'] = $oa;
		}
		$where['web_type']=$_GET['web_type']?$_GET['web_type']:array('IN','0,1');
		$where['p_u_id']=$_SESSION['user']['u_id'];
	   $url=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		$post_url=explode('/',$url);
		foreach ($post_url as $key => $value) {
		  if($value=='p'){
		  	$a=$key;
		  }
		  if($value=='search'){
		  	$b=$key;
		  }
		}
		if($a<$b){
			unset($_GET['p']);
		}
		if(!$_GET['p'])$_get['p']=1;
		$count      = $data->where($where)->count();
		$Page       = new \Think\Page($count,5);
		$show       = $Page->show();
		$list = $data->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		return array("count"=>$count,"list"=>$list,"show"=>$show);
	} 

	public function manage_me_qudao(){

		/******************搜索处理*******************************/
		$data = M('web_user');
		/**
		 * 搜索
		 */
		if($_GET['search']){
			$search=$_GET['search'];
			$oa['qudao_name']=array('like','%'.$search.'%');
			$oa['qudao_nick_name']=array('like','%'.$search.'%');
			$oa['_logic'] = 'or';
			$where['_complex'] = $oa;
		}
		/**
		 * 
		 * 选择类
		 * 
		 */
 		$type=$_GET['type']?$_GET['type']:0;
		switch ($type) {
			case '1':
				$my_type='银行';
				break;
			case '2':
				$my_type='小额贷款';
				break;
			case '3':
				$my_type='消费金融';
				break;
			case '4':
				$my_type='其他';
				break;
			default:
				$my_type='';
				break;
		}

		if($my_type){
			$where['qudao_type']=$my_type;
		}
		$where['web_type']=$_GET['web_type']?$_GET['web_type']:array('IN','2');
		$where['p_u_id']=$_SESSION['user']['u_id'];
		/******************搜索处理*******************************/
	   $url=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	   $post_url=explode('/',$url);
	   foreach ($post_url as $key => $value) {
	      if($value=='p'){
	      	$a=$key;
	      }
	      if($value=='search'){
	      	$b=$key;
	      }
	   }
	   if($a<$b){
			unset($_GET['p']);
	   }
		if(!$_GET['p'])$_get['p']=1;
		$count      = $data
					  ->alias('a')
					  ->join('bm_qudao b ON b.qudao_id=a.qudao_id','LEFT')
					  -> where($where)
					  ->count();
		$Page       = new \Think\Page($count,5);
		$show       = $Page->show();
		$list = $data
				->alias('a')
				->join('bm_qudao b ON b.qudao_id=a.qudao_id','LEFT')
				->where($where)
				->limit($Page->firstRow.','.$Page->listRows)
				->select();
		return array("count"=>$count,"list"=>$list,"show"=>$show);
	}


	public function manage_me_chanpin(){
		/******************搜索处理*******************************/
		$data = M('web_user');
		/**
		 * 搜索
		 */
		if($_GET['search']){
			$search=$_GET['search'];
			$oa['qudao_name']=array('like','%'.$search.'%');
			$oa['qudao_nick_name']=array('like','%'.$search.'%');
			$oa['_logic'] = 'or';
			$where['_complex'] = $oa;
		}
		/**
		 * 
		 * 选择类
		 * 
		 */
 		$type=$_GET['type']?$_GET['type']:0;
		switch ($type) {
			case '1':
				$my_type='银行';
				break;
			case '2':
				$my_type='小额贷款';
				break;
			case '3':
				$my_type='消费金融';
				break;
			case '4':
				$my_type='其他';
				break;
			default:
				$my_type='';
				break;
		}
		if($my_type){
			$where['qudao_type']=$my_type;
		}
		$where['web_type']=$_GET['web_type']?$_GET['web_type']:array('IN','2');
		$where['p_u_id']=$_SESSION['user']['u_id'];
		/******************搜索处理*******************************/
	   $url=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	   $post_url=explode('/',$url);
	   foreach ($post_url as $key => $value) {
	      if($value=='p'){
	      	$a=$key;
	      }
	      if($value=='search'){
	      	$b=$key;
	      }
	   }
	   if($a<$b){
			unset($_GET['p']);
	   }
		if(!$_GET['p'])$_get['p']=1;
		$count      = $data
					  ->alias('a')
					  ->join('bm_qudao b ON b.qudao_id=a.qudao_id','LEFT')
					  -> where($where)
					  ->count();
		$Page       = new \Think\Page($count,5);
		$show       = $Page->show();
		$list = $data
				->alias('a')
				->join('bm_qudao b ON b.qudao_id=a.qudao_id','LEFT')
				->where($where)
				->limit($Page->firstRow.','.$Page->listRows)
				->select();
		return array("count"=>$count,"list"=>$list,"show"=>$show);
	}
/*************以下方法隶属Manageme同名操作****************************/



	/**
	 * ajax 生成二维码/业务员qudao_id=-1 银行职员为正数，对应渠道
	 */
	public function my_ajax($qudao_id,$type){
		
		$where['qudao_id']=$qudao_id;
		if(isset($qudao_id)){
			$find=M('qudao')->where($where)->find();
			if($find){
					$wechat_where['wechat_id']=$_SESSION['user']['wechat_id'];
					$wechat=M('wechat')->where($wechat_where)->find();
					$appid=$wechat['appid'];
					$appsecret=$wechat['appsecret'];
					$api=new \Com\WechatAuth($appid,$appsecret);
					if($type==1){
						$qudao_id=$qudao_id+1000000;
						$link=$qudao_id;
						$res=$api->qrcodeCreate($link);

					}
					elseif($type==2){
						$link=$_SESSION['user']['u_id'].",".$qudao_id;
						$res=$api->textCreat($link);
					}

					$msg['message']="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$res['ticket'];
					$msg['code']=1;
			}
			else{
				if($qudao_id!=0){
					$wechat_where['wechat_id']=$_SESSION['user']['wechat_id'];
					$wechat=M('wechat')->where($wechat_where)->find();
					$appid=$wechat['appid'];
					$appsecret=$wechat['appsecret'];
					$api=new \Com\WechatAuth($appid,$appsecret);
					$link=$qudao_id;
					$res=$api->qrcodeCreate($link);

					$msg['message']="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$res['ticket'];
					$msg['code']=1;
					return $msg;
				}


				$msg['code']=2;
				$msg['message']='渠道选择错误';
			}
		}
		else{
				$msg['code']=3;
				$msg['message']='选择渠道';
		}
		return $msg;
	}

/***************************小类ajax处理方案***********************/
	/**
	 * 录入框架
	 * @return [type] [description]
	 */
	public function sm_ajax(){
		$m4=M('m4_link');
		$m3=M('m3_link');
		$m=M('m_link');
		if($_POST['formData']){
			foreach ($_POST['formData'] as $key1 => $value1) {
				/**
				 *处理单选多选输入
				 * 
				 */
				if(is_array($value1)&&is_array($value1['main_data'])){
					//第三层结构
					$result4=$m4->addAll($value1['main_data']);
					//第二层结构
					$data3['title']=$value1['title'];
					$data3['group']=$value1['group'];
					$data3['main_data']=$value1['group']."_".$key1;
					$data3['input_type']=$value1['input_type'];
					$result3=$m3->add($data3);
				}
				/**
				 *
				 *
				 *处理数字类型输入
				 * 
				 */
				elseif (isset($value1['main_data'])&&!is_array($value1['main_data'])) {
					//第二层结构
					$data3['title']=$value1['title'];
					$data3['group']=$value1['group'];
					$data3['main_data']=$value1['group']."_".$key1;
					$data3['input_type']=$value1['input_type'];
					$result3=$m3->add($data3);
				}


			}
			$data['m_group']=$_POST['m_group'];
			$data['m_value']=$_POST['m_value'];
			$result=$m->add($data);
		}



		return $result;
	}


/***************************小类ajax处理方案***********************/

	public function get_ajax(){
		$where['m_group']=$_POST['m_group'];
		$res=M('m_link')->where($where)->find();
		$where1['group']=$_POST['m_group'];
		$res['formData']=M('m3_link')->where($where1)->select();
		foreach ($res['formData'] as $key => $value) {
			$temp=$res['formData'][$key]['main_data'];
			$where4['index']=$temp;
			$res4=M('m4_link')->where($where4)->select();
			if(is_array($res4)){
				$res['formData'][$key]['main_data']=$res4;
			}
		}
		return $res;


	}






}